package com.Eastiy.EXO.View;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

public class NewSwitch extends View {
    private static int backgroundColor = -3355444;
    private static int colorStep = -3355444;
    private static final long commonDuration = 300;
    private static int foregroundColor = -657931;
    private static int gbolor = -1;
    private static int gbolorbk = -1;
    /* access modifiers changed from: private */
    public int centerX;
    private int centerY;
    private float cornerRadius;
    private boolean dirtyAnimation;
    private GestureDetector gestureDetector;
    private GestureDetector.SimpleOnGestureListener gestureListener;
    private int height;
    /* access modifiers changed from: private */
    public ValueAnimator innerContentAnimator;
    private RectF innerContentBound;
    /* access modifiers changed from: private */
    public float innerContentRate;
    private float intrinsicInnerHeight;
    private float intrinsicInnerWidth;
    private float intrinsicKnobWidth;
    private boolean isAttachedToWindow;
    /* access modifiers changed from: private */
    public boolean isOn;
    private RectF knobBound;
    /* access modifiers changed from: private */
    public ValueAnimator knobExpandAnimator;
    /* access modifiers changed from: private */
    public float knobExpandRate;
    private float knobMaxExpandWidth;
    /* access modifiers changed from: private */
    public ValueAnimator knobMoveAnimator;
    /* access modifiers changed from: private */
    public float knobMoveRate;
    /* access modifiers changed from: private */
    public boolean knobState;
    /* access modifiers changed from: private */
    public OnSwitchStateChangeListener onSwitchStateChangeListener;
    private int outerStrokeWidth;
    private RectF ovalForPath;
    private Paint paint;
    /* access modifiers changed from: private */
    public boolean preIsOn;
    private Path roundRectPath;
    private int shadowSpace;
    private RectF tempForRoundRect;
    private int tempTintColor;
    private int tintColor;
    private int width;

    public interface OnSwitchStateChangeListener {
        void onSwitchStateChange(boolean z);
    }

    public NewSwitch(Context context) {
        this(context, (AttributeSet) null);
    }

    public NewSwitch(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public NewSwitch(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.gestureListener = new GestureDetector.SimpleOnGestureListener(this) {
            private final NewSwitch this$0;

            {
                this.this$0 = r1;
            }

            static NewSwitch access$0(AnonymousClass100000000 r1) {
                return r1.this$0;
            }

            @Override
            public boolean onDown(MotionEvent motionEvent) {
                if (!this.this$0.isEnabled()) {
                    return false;
                }
                this.this$0.preIsOn = this.this$0.isOn;
                this.this$0.innerContentAnimator.setFloatValues(new float[]{this.this$0.innerContentRate, 0.0f});
                this.this$0.innerContentAnimator.start();
                this.this$0.knobExpandAnimator.setFloatValues(new float[]{this.this$0.knobExpandRate, 1.0f});
                this.this$0.knobExpandAnimator.start();
                return true;
            }

            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
                if (motionEvent2.getX() > ((float) this.this$0.centerX)) {
                    if (!this.this$0.knobState) {
                        this.this$0.knobState = !this.this$0.knobState;
                        this.this$0.knobMoveAnimator.setFloatValues(new float[]{this.this$0.knobMoveRate, 1.0f});
                        this.this$0.knobMoveAnimator.start();
                        this.this$0.innerContentAnimator.setFloatValues(new float[]{this.this$0.innerContentRate, 0.0f});
                        this.this$0.innerContentAnimator.start();
                    }
                } else if (this.this$0.knobState) {
                    this.this$0.knobState = !this.this$0.knobState;
                    this.this$0.knobMoveAnimator.setFloatValues(new float[]{this.this$0.knobMoveRate, 0.0f});
                    this.this$0.knobMoveAnimator.start();
                }
                return true;
            }

            @Override
            public void onShowPress(MotionEvent motionEvent) {
            }

            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                this.this$0.isOn = this.this$0.knobState;
                if (this.this$0.preIsOn ? this.this$0.isOn : !this.this$0.isOn) {
                    this.this$0.isOn = !this.this$0.isOn;
                    this.this$0.knobState = !this.this$0.knobState;
                }
                if (this.this$0.knobState) {
                    this.this$0.knobMoveAnimator.setFloatValues(new float[]{this.this$0.knobMoveRate, 1.0f});
                    this.this$0.knobMoveAnimator.start();
                    this.this$0.innerContentAnimator.setFloatValues(new float[]{this.this$0.innerContentRate, 0.0f});
                    this.this$0.innerContentAnimator.start();
                } else {
                    this.this$0.knobMoveAnimator.setFloatValues(new float[]{this.this$0.knobMoveRate, 0.0f});
                    this.this$0.knobMoveAnimator.start();
                    this.this$0.innerContentAnimator.setFloatValues(new float[]{this.this$0.innerContentRate, 1.0f});
                    this.this$0.innerContentAnimator.start();
                }
                this.this$0.knobExpandAnimator.setFloatValues(new float[]{this.this$0.knobExpandRate, 0.0f});
                this.this$0.knobExpandAnimator.start();
                if (this.this$0.onSwitchStateChangeListener != null && (this.this$0.isOn ? !this.this$0.preIsOn : this.this$0.preIsOn)) {
                    this.this$0.onSwitchStateChangeListener.onSwitchStateChange(this.this$0.isOn);
                }
                return true;
            }
        };
        this.innerContentRate = 1.0f;
        this.dirtyAnimation = false;
        this.isAttachedToWindow = false;
        this.tintColor = 3684408;
        this.tempTintColor = this.tintColor;
        this.outerStrokeWidth = (int) (TypedValue.applyDimension(1, (float) 1, context.getResources().getDisplayMetrics()) * ((float) 10));
        this.shadowSpace = (int) (TypedValue.applyDimension(1, (float) 1, context.getResources().getDisplayMetrics()) * ((float) 10));
        this.knobBound = new RectF();
        this.innerContentBound = new RectF();
        this.ovalForPath = new RectF();
        this.tempForRoundRect = new RectF();
        this.paint = new Paint(1);
        this.roundRectPath = new Path();
        this.gestureDetector = new GestureDetector(context, this.gestureListener);
        this.gestureDetector.setIsLongpressEnabled(false);
        if (Build.VERSION.SDK_INT >= 11) {
            setLayerType(1, (Paint) null);
        }
        initAnimators();
    }

    private int RGBColorTransform(float f, int i, int i2) {
        int i3 = (i >> 16) & 255;
        int i4 = (i >> 8) & 255;
        int i5 = i & 255;
        return ((i3 + ((int) (((float) (((i2 >> 16) & 255) - i3)) * f))) << 16) | -16777216 | ((i4 + ((int) (((float) (((i2 >> 8) & 255) - i4)) * f))) << 8) | (((int) (((float) ((i2 & 255) - i5)) * f)) + i5);
    }

    private void drawRoundRect(float f, float f2, float f3, float f4, float f5, Canvas canvas, Paint paint2) {
        this.tempForRoundRect.left = f;
        this.tempForRoundRect.top = f2;
        this.tempForRoundRect.right = f3;
        this.tempForRoundRect.bottom = f4;
        canvas.drawRoundRect(this.tempForRoundRect, f5, f5, paint2);
    }

    private void initAnimators() {
        this.innerContentAnimator = ValueAnimator.ofFloat(new float[]{this.innerContentRate, 1.0f});
        this.knobExpandAnimator = ValueAnimator.ofFloat(new float[]{this.knobExpandRate, 1.0f});
        this.knobMoveAnimator = ValueAnimator.ofFloat(new float[]{this.knobMoveRate, 1.0f});
        this.innerContentAnimator.setDuration(commonDuration);
        this.knobExpandAnimator.setDuration(commonDuration);
        this.knobMoveAnimator.setDuration(commonDuration);
        this.innerContentAnimator.setInterpolator(new DecelerateInterpolator());
        this.knobExpandAnimator.setInterpolator(new DecelerateInterpolator());
        this.knobMoveAnimator.setInterpolator(new DecelerateInterpolator());
        this.innerContentAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(this) {
            private final NewSwitch this$0;

            {
                this.this$0 = r1;
            }

            static NewSwitch access$0(AnonymousClass100000001 r1) {
                return r1.this$0;
            }

            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                this.this$0.setInnerContentRate(((Float) valueAnimator.getAnimatedValue()).floatValue());
            }
        });
        this.knobExpandAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(this) {
            private final NewSwitch this$0;

            {
                this.this$0 = r1;
            }

            static NewSwitch access$0(AnonymousClass100000002 r1) {
                return r1.this$0;
            }

            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                this.this$0.setKnobExpandRate(((Float) valueAnimator.getAnimatedValue()).floatValue());
            }
        });
        this.knobMoveAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(this) {
            private final NewSwitch this$0;

            {
                this.this$0 = r1;
            }

            static NewSwitch access$0(AnonymousClass100000003 r1) {
                return r1.this$0;
            }

            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                this.this$0.setKnobMoveRate(((Float) valueAnimator.getAnimatedValue()).floatValue());
            }
        });
    }

    /* access modifiers changed from: package-private */
    public float getInnerContentRate() {
        return this.innerContentRate;
    }

    /* access modifiers changed from: package-private */
    public float getKnobExpandRate() {
        return this.knobExpandRate;
    }

    /* access modifiers changed from: package-private */
    public float getKnobMoveRate() {
        return this.knobMoveRate;
    }

    public OnSwitchStateChangeListener getOnSwitchStateChangeListener() {
        return this.onSwitchStateChangeListener;
    }

    public int getTintColor() {
        return this.tintColor;
    }

    public int getbackgroundColor() {
        return backgroundColor;
    }

    public int getcolorStep() {
        return colorStep;
    }

    public int getforegroundColor() {
        return foregroundColor;
    }

    public boolean isOn() {
        return this.isOn;
    }

    /* access modifiers changed from: protected */
    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.isAttachedToWindow = true;
        if (this.dirtyAnimation) {
            this.knobState = this.isOn;
            if (this.knobState) {
                this.knobMoveAnimator.setFloatValues(new float[]{this.knobMoveRate, 1.0f});
                this.knobMoveAnimator.start();
                this.innerContentAnimator.setFloatValues(new float[]{this.innerContentRate, 0.0f});
                this.innerContentAnimator.start();
            } else {
                this.knobMoveAnimator.setFloatValues(new float[]{this.knobMoveRate, 0.0f});
                this.knobMoveAnimator.start();
                this.innerContentAnimator.setFloatValues(new float[]{this.innerContentRate, 1.0f});
                this.innerContentAnimator.start();
            }
            this.knobExpandAnimator.setFloatValues(new float[]{this.knobExpandRate, 0.0f});
            this.knobExpandAnimator.start();
            if (this.onSwitchStateChangeListener != null && (this.isOn ? !this.preIsOn : this.preIsOn)) {
                this.onSwitchStateChangeListener.onSwitchStateChange(this.isOn);
            }
            this.dirtyAnimation = false;
        }
    }

    /* access modifiers changed from: protected */
    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.isAttachedToWindow = false;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float f = (this.intrinsicInnerWidth / 2.0f) * this.innerContentRate;
        float f2 = (this.intrinsicInnerHeight / 2.0f) * this.innerContentRate;
        this.innerContentBound.left = ((float) this.centerX) - f;
        this.innerContentBound.top = ((float) this.centerY) - f2;
        this.innerContentBound.right = f + ((float) this.centerX);
        this.innerContentBound.bottom = f2 + ((float) this.centerY);
        float f3 = ((this.knobMaxExpandWidth - this.intrinsicKnobWidth) * this.knobExpandRate) + this.intrinsicKnobWidth;
        if (this.knobBound.left + (this.knobBound.width() / ((float) 2)) > ((float) this.centerX)) {
            this.knobBound.left = this.knobBound.right - f3;
        } else {
            this.knobBound.right = f3 + this.knobBound.left;
        }
        float width2 = this.knobBound.width();
        float f4 = this.knobMoveRate;
        colorStep = RGBColorTransform(this.knobMoveRate, backgroundColor, this.tintColor);
        this.knobBound.left = (((((float) this.width) - width2) - ((float) ((this.shadowSpace + this.outerStrokeWidth) * 2))) * f4) + ((float) (this.shadowSpace + this.outerStrokeWidth));
        this.knobBound.right = width2 + this.knobBound.left;
        this.paint.setColor(colorStep);
        this.paint.setShadowLayer((float) 5, (float) 3, (float) 3, Color.parseColor("#383838"));
        this.paint.setStyle(Paint.Style.FILL);
        drawRoundRect((float) this.shadowSpace, (float) this.shadowSpace, (float) (this.width - this.shadowSpace), (float) (this.height - this.shadowSpace), this.cornerRadius, canvas, this.paint);
        this.paint.setColor(backgroundColor);
        canvas.drawRoundRect(this.innerContentBound, this.innerContentBound.height() / ((float) 2), this.innerContentBound.height() / ((float) 2), this.paint);
        this.paint.setColor(foregroundColor);
        this.paint.setShadowLayer((float) 2, (float) 0, (float) (this.shadowSpace / 10), isEnabled() ? 536870912 : 268435456);
        canvas.drawRoundRect(this.knobBound, this.cornerRadius - ((float) this.outerStrokeWidth), this.cornerRadius - ((float) this.outerStrokeWidth), this.paint);
        this.paint.setShadowLayer((float) 0, (float) 0, (float) 0, 0);
    }

    /* access modifiers changed from: protected */
    @Override
    public void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        this.width = View.MeasureSpec.getSize(i);
        this.height = View.MeasureSpec.getSize(i2);
        if (((float) this.height) / ((float) this.width) < 0.33333f) {
            this.height = (int) (((float) this.width) * 0.33333f);
            super.setMeasuredDimension(View.MeasureSpec.makeMeasureSpec(this.width, View.MeasureSpec.getMode(i)), View.MeasureSpec.makeMeasureSpec(this.height, View.MeasureSpec.getMode(i2)));
        }
        this.centerX = this.width / 2;
        this.centerY = this.height / 2;
        this.cornerRadius = (float) (this.centerY - this.shadowSpace);
        this.innerContentBound.left = (float) (this.outerStrokeWidth + this.shadowSpace);
        this.innerContentBound.top = (float) (this.outerStrokeWidth + this.shadowSpace);
        this.innerContentBound.right = (float) ((this.width - this.outerStrokeWidth) - this.shadowSpace);
        this.innerContentBound.bottom = (float) ((this.height - this.outerStrokeWidth) - this.shadowSpace);
        this.intrinsicInnerWidth = this.innerContentBound.width();
        this.intrinsicInnerHeight = this.innerContentBound.height();
        this.knobBound.left = (float) (this.outerStrokeWidth + this.shadowSpace);
        this.knobBound.top = (float) (this.outerStrokeWidth + this.shadowSpace);
        this.knobBound.right = (float) ((this.height - this.outerStrokeWidth) - this.shadowSpace);
        this.knobBound.bottom = (float) ((this.height - this.outerStrokeWidth) - this.shadowSpace);
        this.intrinsicKnobWidth = this.knobBound.height();
        this.knobMaxExpandWidth = ((float) this.width) * 0.6f;
        if (this.knobMaxExpandWidth > this.knobBound.width() * 1.25f) {
            this.knobMaxExpandWidth = this.knobBound.width() * 1.25f;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (!isEnabled()) {
            return false;
        }
        switch (motionEvent.getAction()) {
            case 1:
            case 3:
                if (!this.knobState) {
                    this.innerContentAnimator.setFloatValues(new float[]{this.innerContentRate, 1.0f});
                    this.innerContentAnimator.start();
                }
                this.knobExpandAnimator.setFloatValues(new float[]{this.knobExpandRate, 0.0f});
                this.knobExpandAnimator.start();
                this.isOn = this.knobState;
                if (this.onSwitchStateChangeListener != null && (this.isOn ? !this.preIsOn : this.preIsOn)) {
                    this.onSwitchStateChangeListener.onSwitchStateChange(this.isOn);
                    break;
                }
        }
        return this.gestureDetector.onTouchEvent(motionEvent);
    }

    @Override
    public void setEnabled(boolean z) {
        super.setEnabled(z);
        if (z) {
            this.tintColor = this.tempTintColor;
        } else {
            this.tintColor = RGBColorTransform(0.5f, gbolorbk, gbolor);
        }
    }

    /* access modifiers changed from: package-private */
    public void setInnerContentRate(float f) {
        this.innerContentRate = f;
        invalidate();
    }

    /* access modifiers changed from: package-private */
    public void setKnobExpandRate(float f) {
        this.knobExpandRate = f;
        invalidate();
    }

    /* access modifiers changed from: package-private */
    public void setKnobMoveRate(float f) {
        this.knobMoveRate = f;
        invalidate();
    }

    public void setOn(boolean z) {
        setOn(z, false);
    }

    public void setOn(boolean z, boolean z2) {
        if (!this.isOn) {
            if (!z) {
                return;
            }
        } else if (z) {
            return;
        }
        if (this.isAttachedToWindow || !z2) {
            this.isOn = z;
            this.knobState = this.isOn;
            if (!z2) {
                if (z) {
                    setKnobMoveRate(1.0f);
                    setInnerContentRate(0.0f);
                } else {
                    setKnobMoveRate(0.0f);
                    setInnerContentRate(1.0f);
                }
                setKnobExpandRate(0.0f);
            } else {
                if (this.knobState) {
                    this.knobMoveAnimator.setFloatValues(new float[]{this.knobMoveRate, 1.0f});
                    this.knobMoveAnimator.start();
                    this.innerContentAnimator.setFloatValues(new float[]{this.innerContentRate, 0.0f});
                    this.innerContentAnimator.start();
                } else {
                    this.knobMoveAnimator.setFloatValues(new float[]{this.knobMoveRate, 0.0f});
                    this.knobMoveAnimator.start();
                    this.innerContentAnimator.setFloatValues(new float[]{this.innerContentRate, 1.0f});
                    this.innerContentAnimator.start();
                }
                this.knobExpandAnimator.setFloatValues(new float[]{this.knobExpandRate, 0.0f});
                this.knobExpandAnimator.start();
            }
            if (this.onSwitchStateChangeListener != null) {
                if (!this.isOn) {
                    if (!this.preIsOn) {
                        return;
                    }
                } else if (this.preIsOn) {
                    return;
                }
                this.onSwitchStateChangeListener.onSwitchStateChange(this.isOn);
                return;
            }
            return;
        }
        this.dirtyAnimation = true;
        this.isOn = z;
    }

    public void setOnSwitchStateChangeListener(OnSwitchStateChangeListener onSwitchStateChangeListener2) {
        this.onSwitchStateChangeListener = onSwitchStateChangeListener2;
    }

    public void setTintColor(int i) {
        this.tintColor = i;
        this.tempTintColor = this.tintColor;
    }

    public void setbackgroundColor(int i) {
        backgroundColor = i;
    }

    public void setcolorStep(int i) {
        colorStep = i;
    }

    public void setforegroundColor(int i) {
        foregroundColor = i;
    }
}
